package com.tnovoselec.android.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.tnovoselec.android.ui.MyViewPager.OnPagerAdapterChangedListener;

public class MyViewPagerTab2 extends HorizontalScrollView implements OnPageChangeListener, OnPagerAdapterChangedListener, OnClickListener {

	private RadioGroup radiogroup;

	private PagerAdapter mAdapter;
	private MyViewPager nthViewPager;
	private int nthViewPagerId;
	private int tabLayoutId;

	public MyViewPagerTab2(Context context) {
		this(context, null);
	}

	public MyViewPagerTab2(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public MyViewPagerTab2(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		setFadingEdgeLength((int) (35 * getResources().getDisplayMetrics().density));
		setHorizontalFadingEdgeEnabled(true);
		try {
			TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyViewPagerTab2, defStyle, 0);
			nthViewPagerId = a.getResourceId(R.styleable.MyViewPagerTab2_myViewPagerId, View.NO_ID);
			tabLayoutId = a.getResourceId(R.styleable.MyViewPagerTab2_tabLayout, R.layout.my_view_pager_tab_2_indicator);
			a.recycle();
		} catch (Exception e) {
			Log.e("ss", e.getMessage());
		}

		radiogroup = new RadioGroup(getContext());
		radiogroup.setOrientation(LinearLayout.HORIZONTAL);
		addView(radiogroup, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
	}

	@Override
	protected void onAttachedToWindow() {
		super.onAttachedToWindow();

		if (nthViewPagerId != View.NO_ID) {
			nthViewPager = (MyViewPager) getRootView().findViewById(nthViewPagerId);
			setNthViewPager(nthViewPager);
		}
	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
		if (nthViewPager != null) {
			onPageSelected(nthViewPager.getCurrentItem());
		}
	}

	@Override
	public void onPageScrollStateChanged(int state) {
	}

	@Override
	public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
	}

	@Override
	public void onPageSelected(int position) {
		final View v = radiogroup.getChildAt(position);
		if (v != null) {
			radiogroup.check(v.getId());
			smoothScrollTo(v.getLeft() - (getWidth() - v.getWidth()) / 2, 0);
		}
	}

	@Override
	public void onPagerAdapterChanged(MyViewPager viewPager) {
		if (viewPager != null) {
			setAdapter(viewPager.getAdapter());
		}
	}

	@Override
	public void onClick(View v) {
		try {
			nthViewPager.setCurrentItem(radiogroup.indexOfChild(v));
		} catch (Exception e) {
			Log.e("error", e.getMessage());
		}
	}

	public void setAdapter(PagerAdapter adapter) {
		mAdapter = adapter;

		radiogroup.removeAllViews();
		if (mAdapter != null) {
			for (int i = 0; i < mAdapter.getCount(); i++) {
				final View v = getTabIndicator(i, mAdapter.getPageTitle(i));
				radiogroup.addView(v);
			}
			requestLayout();
		}
	}

	public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
		if (radiogroup != null) {
			radiogroup.setOnCheckedChangeListener(listener);
		}
	}

	public void setNthViewPager(MyViewPager viewPager) {
		nthViewPager = viewPager;
		if (nthViewPager != null) {
			nthViewPager.addOnPageChangeListener(this);
			nthViewPager.addOnPagerAdapterChangedListeners(this);
			onPagerAdapterChanged(nthViewPager);
		}
	}

	protected View getTabIndicator(final int position, CharSequence title) {
		final RadioButton radioButton = (RadioButton) LayoutInflater.from(getContext()).inflate(tabLayoutId, radiogroup, false);
		radioButton.setText(title);
		radioButton.setOnClickListener(this);
		return radioButton;
	}

}
